<?php

namespace App\Http\Controllers\Api;

use App\Models\Collection;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CollectionController extends Controller
{
    public function lists(Request $request)
    {

        $collections = Collection::where('user_id',$request->get('user')->user_id)
            ->orderBy('created_at','desc')
            ->get();
        foreach ($collections as $collection){
            $goods = $collection->my_goods;
            $goods ->cover = $goods->my_picture()->where('type',1)->first()->my_pic_path;
        }
        return $collections;
    }

    public function create(Request $request)
    {
        $data=[
            'user_id'=>$request->get('user')->user_id,
            'goods_id'=>$request->get('goods_id'),
        ];
        Collection::create($data);
        return ['status_code'=>1,'message'=>"加入成功"];
    }

    public function destory(Request $request)
    {

        if($request->has('collection_id')){
            Collection::destroy($request->get('collection_id'));
        }else{
            Collection::where([['user_id',$request->get('user')->user_id],['goods_id',$request->get('goods_id')]])->delete();
        }
        return ['status_code'=>1,'message'=>"删除成功"];
    }

}
